<template>
  <div class="dashboard-container">
    <div class="app-container">
      <!-- 实现页面的基本布局 -->
      <upload-excel :on-success="success" />
    </div>
  </div>
</template>
<script>
import { importEmployee } from '@/api/employees'
export default {
  methods: {
    async success({ header, results }) {
      // console.log(results)
      var userRelations = {
        '入职日期': 'timeOfEntry',
        '姓名': 'username',
        '工号': 'workNumber',
        '手机号': 'mobile',
        '转正日期': 'correctionTime'
      }
      const newArr = results.map(item => {
        // console.log(Object.keys(item))
        const userinfo = {}
        Object.keys(item).forEach(key => {
          // console.log(userRelations[key])
          // console.log(item[key])
          if (userRelations[key] === 'timeOfEntry' || userRelations[key] === 'correctionTime') {
            // 只有转换成时间戳的形式 才能入库
            userinfo[userRelations[key]] = new Date(this.formatDate(item[key], '/'))
          } else {
            userinfo[userRelations[key]] = item[key]
          }
        })
        // console.log(userinfo)
        return userinfo
      })
      // console.log(newArr)
      await importEmployee(newArr)
      this.$message.success('导入成功')
      this.$router.back() // 回到上一页
    },
    formatDate(numb, format) {
      const time = new Date((numb - 1) * 24 * 3600000 + 1)
      time.setYear(time.getFullYear() - 70)
      const year = time.getFullYear() + ''
      const month = time.getMonth() + 1 + ''
      const date = time.getDate() - 1 + ''
      if (format && format.length === 1) {
        return year + format + month + format + date
      }
      return year + (month < 10 ? '0' + month : month) + (date < 10 ? '0' + date : date)
    }
  }

}
</script>
<style >

</style>
